package com.spremoteservice.android;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.xml.sax.SAXException;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityDocument extends Activity {
	ListViewSPObject _spObj = null;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
		setContentView(R.layout.activity_document);
		
        Intent intent = getIntent();
        ListViewSPObject spObj = (ListViewSPObject)intent.getSerializableExtra("ListViewSPObjectParent");
        _spObj = spObj;
        
    	TextView tvDocName = (TextView) findViewById(R.id.doc_name);
    	tvDocName.setText(spObj.documentName);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	            case R.id.action_settings:
		            Intent i = new Intent(this, ActivitySettings.class);
		            startActivity(i);
	                return true;
	            default:
	                return super.onOptionsItemSelected(item);
	    }
	}
	
	public void openFile(View view){
		CallAsyncOpenFile task = new CallAsyncOpenFile();
		task.execute(new String[]{_spObj.siteUrl, _spObj.folders});
		try {
			task.get(120000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Boolean b = task.GetResult();
		if(b){
			Toast.makeText(getApplicationContext(), "File is open", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "Cannot open file", Toast.LENGTH_SHORT).show();
		}

	}

	public void pageUp(View view){
		CallAsyncPageUp task = new CallAsyncPageUp();
		task.execute(new String[]{});
		try {
			task.get(120000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Boolean b = task.GetResult();
		if(b){
			Toast.makeText(getApplicationContext(), "Page up", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
		}

		
	}
	public void pageDown(View view){
		CallAsyncPageDown task = new CallAsyncPageDown();
		task.execute(new String[]{});
		try {
			task.get(120000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Boolean b = task.GetResult();
		if(b){
			Toast.makeText(getApplicationContext(), "Page down", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
		}

		
	}
	public void closeFile(View view){
		CallAsyncCloseFile task = new CallAsyncCloseFile();
		task.execute(new String[]{});
		try {
			task.get(120000, TimeUnit.MILLISECONDS);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TimeoutException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Boolean b = task.GetResult();
		if(b){
			Toast.makeText(getApplicationContext(), "File is close", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(getApplicationContext(), "Cannot close file", Toast.LENGTH_SHORT).show();
		}

	}
}

class CallAsyncOpenFile extends AsyncTask<String, Void, Boolean>{
	
	private Boolean result;
	
	public Boolean GetResult(){
		return result;
	}
	
	@Override
	protected Boolean doInBackground(String... arg0) {
		String siteUrl = arg0[0];
		String filePath = arg0[1];
		result = false;
		Connector connector = Connector.GetInstance();
		try {
			if(connector.OpenFile(siteUrl, filePath)){
				result = true;
			}else{
				result = false;			
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;	
	}
	
}
class CallAsyncPageDown extends AsyncTask<String, Void, Boolean>{
	
	private Boolean result;
	
	public Boolean GetResult(){
		return result;
	}
	
	@Override
	protected Boolean doInBackground(String... arg0) {
		result = false;
		Connector connector = Connector.GetInstance();
		try {
			if(connector.PageDown()){
				result = true;
			}else{
				result = false;			
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;	
	}
	
}
class CallAsyncPageUp extends AsyncTask<String, Void, Boolean>{
	
	private Boolean result;
	
	public Boolean GetResult(){
		return result;
	}
	
	@Override
	protected Boolean doInBackground(String... arg0) {
		result = false;
		Connector connector = Connector.GetInstance();
		try {
			if(connector.PageUp()){
				result = true;
			}else{
				result = false;			
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;	
	}
	
}
class CallAsyncCloseFile extends AsyncTask<String, Void, Boolean>{
	
	private Boolean result;
	
	public Boolean GetResult(){
		return result;
	}
	
	@Override
	protected Boolean doInBackground(String... arg0) {
		result = false;
		Connector connector = Connector.GetInstance();
		try {
			if(connector.CloseFile()){
				result = true;
			}else{
				result = false;			
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;	
	}
	
}
